<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input style="width: 200px" v-model="params.paper_text" placeholder="请输入试题内容"></el-input>
      <el-input style="margin-left: 10px;width: 200px" v-model="params.typeName" placeholder="请输入试题分类"></el-input>
      <el-button type="info" style="margin-left: 10px" plain @click="findBySearch()">查询</el-button>
      <el-button type="warning" style="margin-left: 10px" plain @click="reset()">清空</el-button>
      <el-button type="primary" style="margin-left: 10px" plain @click="add()"v-if="user.role==='管理员'">新增</el-button>
      <el-popconfirm title="确定删除这些数据吗？" @confirm="delBatch()">
        <el-button slot="reference" size="mini" type="danger" style="float: right;margin-left: 10px" v-if="user.role==='管理员'">批量删除</el-button>
      </el-popconfirm>
      <el-button slot="reference" size="mini" type="success" style="float: right;margin-left: 10px" plain @click="exp()" >导出题库</el-button>
      <el-upload
          action="http://localhost:8080/zcy/paper/upload"
          style="float: right"
          :show-file-list="false"
          :on-success="success_Upload">
        <el-button size="mini"v-if="user.role==='管理员'">点击上传</el-button>
      </el-upload>
    </div>
    <div>
      <el-table
          :data="tableData"
          stripe
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
            v-if="user.role==='管理员'">
        </el-table-column>
        <el-table-column
            prop="paper_text"
            label="试题内容"
            width="400">
        </el-table-column>
        <el-table-column
            prop="paper_type"
            label="试题类型"
            width="120">
        </el-table-column>
        <el-table-column
            prop="typeName"
            label="试题分类"
            width="120">
        </el-table-column>
        <el-table-column
            prop="answer"
            label="答案"
            width="120">
        </el-table-column>
        <el-table-column
            label="图片"
            width="160">
          <template v-slot="scope">
            <el-image
                style="width: 60px; height: 60px"
                :src="'http://localhost:8080/zcy/files/'+scope.row.img"
                :preview-src-list="['http://localhost:8080/zcy/files/'+scope.row.img]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button size="mini" @click="edit(scope.row)"v-if="user.role==='管理员'">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" size="mini" type="danger" style="margin-left: 10px"v-if="user.role==='管理员'">删除</el-button>
            </el-popconfirm>
            <el-button size="mini" @click="down(scope.row.img)" type="warning" style="margin-left: 10px">下载图片</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block" style="margin-top: 15px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div>
      <el-dialog title="请输入信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form">
          <el-form-item label="试题内容" :label-width="formLabelWidth">
            <el-input v-model="form.paper_text" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="试题类型" :label-width="formLabelWidth">
            <template>
              <el-radio v-model="form.paper_type" label="判断">判断</el-radio>
              <el-radio v-model="form.paper_type" label="选择">选择</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="试卷分类" :label-width="formLabelWidth">
            <el-select v-model="form.typeId" placeholder="请选择">
              <el-option
                  v-for="item in typeObjs"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="答案" :label-width="formLabelWidth">
            <el-input v-model="form.answer" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="图片" :label-width="formLabelWidth">
            <el-upload
                action="http://localhost:8080/zcy/files/upload"
                :on-success="successUpload">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  data() {
    return {
      params:{
        paper_text:'',
        typeName:'',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form:{},
      formLabelWidth:"20%",
      typeObjs:[],
      multipleSelection: [],
      user: localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
    }
  },
  created() {
    this.findTypes();
    this.findBySearch();
  },
  methods:{
    findTypes(){
      request.get("/type").then(res=>{
        if (res.code === '0'){
          this.typeObjs = res.data;
        }else{
          this.$message.error({
            message: '无图片！',
            type: 'error'
          })
        }
      })
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    exp(){
      let user = localStorage.getItem("user");
      location.href='http://localhost:8080/zcy/paper/export?token='+ JSON.parse(user).token;
    },
    success_Upload(res) {
      if(res.code=='0'){
        this.$message({
          message: '成功',
          type: 'success'
        })
        this.findBySearch();
      }else{
        this.$message.error({
          message: res.msg,
          type: 'error'
        })
      }
    },
    delBatch(){
      if(this.multipleSelection.length==0){
        this.$message.warning("请选择要删除的数据!")
        return
      }
      request.put("/paper/delBatch",this.multipleSelection).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: '成功',
            type: 'success'
          })
          this.findBySearch();
        }else {
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    down(flag){
      if (flag!=null){
        location.href='http://localhost:8080/zcy/files/'+flag
      }else{
        this.$message.error({
          message: res.msg,
          type: 'error'
        })
      }
    },
    successUpload(res){
      this.form.img=res.data;
    },
    findBySearch(){
      request.get("/paper/search",{
        params: this.params
      }).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    edit(obj){
      this.form=obj;
      this.dialogFormVisible=true;
    },
    add(){
      this.form= {};
      this.dialogFormVisible=true;
    },
    reset(){
      this.params = {
        paper_text:'',
        typeName:'',
        pageNum: 1,
        pageSize: 10
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    submit(){
      request.post("/paper",this.form).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: '成功',
            type: 'success'
          })
          this.dialogFormVisible=false;
          this.findBySearch();
        }else {
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    del(id){
      request.delete("/paper/"+id).then(res=>{
        if(res.code == '0'){
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.findBySearch();
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
